/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "EntryEntryAbilityViewController.h"
#import <WebKit/WebKit.h>
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
#import <PhotosUI/PHPicker.h>

typedef void(^CallBack)(NSArray<NSString *> * _Nonnull results, int errorCode);

@interface EntryEntryAbilityViewController ()<WKUIDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation EntryEntryAbilityViewController

- (instancetype)initWithInstanceName:(NSString *)instanceName {
    self = [super initWithInstanceName:instanceName];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = YES;
}

- (void)select {
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    if (rootViewController == nil) {
        return;
    }
    if (@available(iOS 14, *)) {
        PHPickerViewController *picker = [self findPHPickerInViewController:rootViewController];
        if (picker) {
            [self selectImageInPicker:picker];
            [picker dismissViewControllerAnimated:true completion:nil];
        }
    } else {
        UIImagePickerController *picker = [self findPHPickerInViewController:rootViewController];
        if (picker) {
            [self selectImageInPicker:picker];
            [picker dismissViewControllerAnimated:true completion:nil];
        }
    }
}

- (id)findPHPickerInViewController:(UIViewController *)viewController {
    if (!viewController) {
        return nil;
    }

    NSLog(@"Current top controller: %@", NSStringFromClass([viewController class]));

    if (@available(iOS 14, *)) {
        if ([viewController isKindOfClass:[PHPickerViewController class]]) {
            return (PHPickerViewController *)viewController;
        } else {
            if ([viewController.presentingViewController isKindOfClass:[PHPickerViewController class]]) {
                return (PHPickerViewController *)viewController.presentingViewController;
            }
        }
    } else {
        if ([viewController isKindOfClass:[UIImagePickerController class]]) {
            return (UIImagePickerController *)viewController;
        } else {
            if ([viewController.presentingViewController isKindOfClass:[UIImagePickerController class]]) {
                return (UIImagePickerController *)viewController.presentingViewController;
            }
        }
    }
    return [self findPHPickerInViewController:viewController.presentedViewController];
}

- (void)selectImageInPicker:(id)picker {
    NSObject *obj;
    if (@available(iOS 14, *)) {
        if ([picker isKindOfClass:[PHPickerViewController class]]) {
            PHPickerViewController * controller = (PHPickerViewController *)picker;
            obj = controller.delegate;
        }
    } else {
        if ([picker isKindOfClass:[UIImagePickerController class]]) {
            UIImagePickerController * controller = (UIImagePickerController *)picker;
            obj = controller.delegate;
        }
    }
    Ivar ivar = class_getInstanceVariable([obj class], "_currentPhotoPickerResult");
    if (!ivar) {
        return;
    }

    CallBack block = object_getIvar(obj, ivar);
    if (block) {
        block(@[@"file:///var/mobile/Media/DCIM/100APPLE/IMG_0714.PNG"], 0);
    } else {
        NSLog(@"Block is nil");
    }
}

@end
